package sy6_Behavioral_Pattern.State;

// State.java
interface State {
    void handle(Context context);
}

// 绿灯
class ConcreteStateA implements State {
    @Override
    public void handle(Context context) {
        System.out.println("当前信号灯状态为绿灯，可以通行。");
        context.setState(new ConcreteStateB());
    }
}

// 黄灯
class ConcreteStateB implements State {
    @Override
    public void handle(Context context) {
        System.out.println("当前信号灯状态为黄灯，请减速。");
        context.setState(new ConcreteStateC());
    }
}

// 红灯
class ConcreteStateC implements State {
    @Override
    public void handle(Context context) {
        if(context.isCongestion){
            System.out.println("当前信号灯状态为红灯,交通拥堵，红灯时常翻倍,请停车等待。");
        }
        else {
            System.out.println("当前信号灯状态为红灯，请停车等待。");
        }
        context.setState(new ConcreteStateA());
    }
}

// Context.java
class Context {
    private State state;
    protected boolean isCongestion;

    public Context() {
        this.state = new ConcreteStateA();
    }

    public void setState(State state) {
        this.state = state;
    }
    public void setCongestion(boolean isCongestion) {
        this.isCongestion = isCongestion;
    }
    public void request() {
        state.handle(this);
    }
}

// Main.java
public class Client {
    public static void main(String[] args) {
        Context context = new Context();

        while (true) {
            context.request();
            try {
                Thread.sleep(2000); // 2秒钟的延迟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}



